package com.mvvm.ktnet.ui.amap

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.location.GnssStatus
import android.location.GpsSatellite
import android.location.GpsStatus
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import com.android.ktx.common.isNull
import com.mvvm.ktnet.R
import com.mvvm.ktnet.base.myappbase.BaseDbActivity
import com.mvvm.ktnet.databinding.AcAmapBinding


class AmapActivity : BaseDbActivity<AmapViewmodel, AcAmapBinding>() {
    override fun layoutId(): Int = R.layout.ac_amap

    lateinit var mService: LocationManager

    var startCount: (Int) -> Unit = {}

    var callBack = @RequiresApi(Build.VERSION_CODES.N)
    object : GnssStatus.Callback() {
        override fun onStarted() {
            super.onStarted()
        }

        override fun onStopped() {
            super.onStopped()
        }

        override fun onFirstFix(ttffMillis: Int) {
            super.onFirstFix(ttffMillis)
        }

        override fun onSatelliteStatusChanged(status: GnssStatus?) {
            super.onSatelliteStatusChanged(status)
            fetchCurGnssStatus(status)
        }
    }

    var listion = GpsStatus.Listener() {
        when (it) {
            GpsStatus.GPS_EVENT_STARTED -> {
            }
            GpsStatus.GPS_EVENT_STOPPED -> {
            }
            GpsStatus.GPS_EVENT_FIRST_FIX -> {
            }
            GpsStatus.GPS_EVENT_SATELLITE_STATUS -> {
                fetchCurGpsStatus()
            }
        }
    }


    @SuppressLint("MissingPermission")
    val launcher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
        if (it) {
            gpsListener()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mService = getSystemService(Context.LOCATION_SERVICE) as LocationManager
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            launcher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
            return
        } else {
            gpsListener()
        }
    }

    @SuppressLint("MissingPermission")
    fun gpsListener() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            mService.registerGnssStatusCallback(callBack)
        } else {
            mService.addGpsStatusListener(listion)
        }
    }

    private fun removeListen() {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            mService.unregisterGnssStatusCallback(callBack)
        } else {
            mService.removeGpsStatusListener(listion)
        }
    }


    @RequiresApi(Build.VERSION_CODES.N)
    private fun fetchCurGnssStatus(status: GnssStatus?) {
        gpsCount(status?.satelliteCount ?: 0)
    }

    fun gpsCount(count: Int) {

        startCount.invoke(count)
    }


    @SuppressLint("MissingPermission")
    private fun fetchCurGpsStatus() {
        val mStatus = mService.getGpsStatus(null)
        //获取卫星颗数的默认最大值
        val maxSatellites = mStatus.maxSatellites
        //创建一个迭代器保存所有卫星
        val iters: Iterator<GpsSatellite> = mStatus.satellites.iterator()
        //卫星数
        var count = 0
        if (iters != null) {
            while (iters.hasNext() && count <= maxSatellites) {
                val s = iters.next()
                if (s.usedInFix()) {
                    count++
                }
            }
        }
        gpsCount(count)
    }


    override fun createObserver() {

    }

    override fun initData() {

    }
}